from __future__ import annotations

import arcade
import game.config as config
from game.entities.snake_entity import SnakeEntity
from game.entities.ground_entity import GroundEntity
import datetime


class GameView(arcade.View):

    def __init__(self):
        # 初始化窗口大小
        super().__init__(background_color=config.COLOR_BACKGROUND)
        self.last_update=datetime.datetime.now()
        self.ground=GroundEntity()
        
        pass

    def on_show_view(self):
        pass

    def on_update(self, delta_time):
        # 周期更新数据
        now = datetime.datetime.now()
        if (now - self.last_update).total_seconds() >= 0.5:
            # self.time_text = now.strftime("%H:%M:%S")
            self.last_update = now
        else:
            return

        self.ground.go()
        pass

    def on_draw(self):
        self.clear()
        # 画面板

        self.ground.draw()
    
    def on_key_press(self, symbol, modifiers):
        # 根据键位判断上下左右
        # print(f'symbol:{symbol}')
        if symbol==arcade.key.UP:
            self.ground.snake.change_direction(config.DIRECTION_UP)
        elif symbol==arcade.key.DOWN:
            self.ground.snake.change_direction(config.DIRECTION_BOTTOM)
        elif symbol==arcade.key.LEFT:
            self.ground.snake.change_direction(config.DIRECTION_LEFT)
        elif symbol==arcade.key.RIGHT:
            self.ground.snake.change_direction(config.DIRECTION_RIGHT)



def main():
    window=arcade.Window(config.SCREEN_WIDTH, config.SCREEN_HEIGHT, config.SCREEN_TITLE)

    window.show_view(GameView())

    arcade.run()

def test_main():
    print('test main')

if __name__=='__main__':
    # print(config.SCREEN_HEIGHT)
    main()
    pass